<?php
//----------------------------------------------------------------------------------------------------------
   require_once('../config.inc.php');
   
   require_once($config['app_dir'] . '/includes/session.class.php');

   require_once($config['app_dir'] . '/classes/sparkline.view.php');
   require_once($config['app_dir'] . '/classes/sparkline.data.php');
   //------------------------------------------------------------------------------------------------------
   try {
      //---------------------------------------------------------------------------------------------------
      // checking input parameters
      //---------------------------------------------------------------------------------------------------      
      $IDParam = new objIDParam();
      if( $IDParam->id ) { //open by id
         $Session = new objSession($config['app_id']);
         $data = $Session->get('sparklines_'.$IDParam->id);

         if( empty($data) ) exit;
         
         $SparkLineView = new objSparkLineView(100, 40);      
         $SparkLineView->SetData($data);
         //---------------------------------------------------------------------------------------------------
         // show image
         //---------------------------------------------------------------------------------------------------
         $SparkLineView -> Render();
         //CLear session
         $Session->clear('sparklines_'.$IDParam->id);
      } else {   //open by params      
         $SparkLineView = new objSparkLineView(100, 40, 5*364*60*24);      //days caching
         if( !$SparkLineView->is_cached ) { //if image not cached before
            //---------------------------------------------------------------------------------------------------
            // preparing data for graph
            //---------------------------------------------------------------------------------------------------
            $SparkLineParams = new objSparkLineParams();
            $SparkLineModel = new objSparkLineModel($SparkLineParams->trade,
                                                    $SparkLineParams->start,
                                                    $SparkLineParams->finish,
                                                    $SparkLineParams->t);
//          $result = $SparkLineModel->Period();
//          var_dump($result);
            $SparkLineView->SetData($SparkLineModel->GetData());
         } //if not cached
         //---------------------------------------------------------------------------------------------------
         // show image
         //---------------------------------------------------------------------------------------------------
         $SparkLineView -> Render();
      }      
    } catch (Exception $e) { //catch errors
      echo 'Error: ',  $e->getMessage(), "\n";
   }
   //-------------------------------------------------------------------------------------------------------
?>
